import { IsEnum, IsOptional, IsString, IsBoolean, IsInt, Min, Max } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { RoleType } from '../../../entities/role.entity';

export enum RoleSortField {
  CREATED_AT = 'createdAt',
  NAME = 'name',
  CODE = 'code'
}

export enum SortOrder {
  ASC = 'ASC',
  DESC = 'DESC'
}

export class QueryRoleDto {
  @ApiPropertyOptional({ description: '角色名称关键词', example: '管理员' })
  @IsOptional()
  @IsString({ message: '角色名称必须是字符串' })
  keyword?: string;

  @ApiPropertyOptional({ description: '角色类型', enum: RoleType })
  @IsOptional()
  @IsEnum(RoleType, { message: '无效的角色类型' })
  type?: RoleType;

  @ApiPropertyOptional({ description: '是否启用', example: true })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean({ message: '是否启用必须是布尔值' })
  isActive?: boolean;

  @ApiPropertyOptional({ description: '排序字段', enum: RoleSortField, default: RoleSortField.CREATED_AT })
  @IsOptional()
  @IsEnum(RoleSortField, { message: '无效的排序字段' })
  sortBy?: RoleSortField = RoleSortField.CREATED_AT;

  @ApiPropertyOptional({ description: '排序方向', enum: SortOrder, default: SortOrder.DESC })
  @IsOptional()
  @IsEnum(SortOrder, { message: '无效的排序方向' })
  sortOrder?: SortOrder = SortOrder.DESC;

  @ApiPropertyOptional({ description: '页码', minimum: 1, default: 1 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsInt({ message: '页码必须是整数' })
  @Min(1, { message: '页码最小为1' })
  page?: number = 1;

  @ApiPropertyOptional({ description: '每页数量', minimum: 1, maximum: 100, default: 10 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsInt({ message: '每页数量必须是整数' })
  @Min(1, { message: '每页数量最小为1' })
  @Max(100, { message: '每页数量最大为100' })
  pageSize?: number = 10;
} 